import * as path from 'path';
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'


// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  plugins: [
    vue(),
  ],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  server: {
    host: '0.0.0.0',
    port: 19000,
    proxy: {
      '/api_url': {
        target: 'http://180.184.86.6:20091',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api_url/, '')
      },
    }
  },
  css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${path.resolve("src/assets/css/base.less")}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
  optimizeDeps: {
    include: [
      `monaco-editor/esm/vs/language/json/json.worker`,
      `monaco-editor/esm/vs/language/css/css.worker`,
      `monaco-editor/esm/vs/language/html/html.worker`,
      `monaco-editor/esm/vs/language/typescript/ts.worker`,
      `monaco-editor/esm/vs/editor/editor.worker`
    ],
  },
  build: {
    outDir: "dist",
    assetsDir: "static", //指定静态资源存放路径
    sourcemap: false, //是否构建source map 文件
    minify: "terser", // 混淆器，terser 构建后文件体积更小，'terser' | 'esbuild'
    chunkSizeWarningLimit: 5120, //chunk 大小警告的限制，默认500KB
    rollupOptions: {
          output: {
            assetFileNames: (chunkInfo) => {
              if (chunkInfo.type === 'asset' && /\.(jpe?g|png|gif|svg)$/i.test(chunkInfo?.name?? '')) {
                return 'static/img/[name].[hash].[ext]';
              }   
              if (chunkInfo.type === 'asset' && /\.(ttf|woff|woff2|eot)$/i.test(chunkInfo?.name?? '')) {
                return 'static/fonts/[name].[hash].[ext]';
              }
              return 'static/[ext]/[name].[hash].[ext]';
            },
         }
    },
    terserOptions: {
      // 生产环境移除console
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
      // 10月更新
      output: {
         comments: true, // 去掉注释内容
      },
    },
  },
})
